﻿// ***********************************************************************
// Assembly         : SecurityLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="ResetForgottenPasswordController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using InnovaApp.Layers.SecurityLayer.Models.ViewModels._LayoutViewModels;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Extensions;
using InnovaApp.Portals.MVC4Portal.Models.ViewModels.LayoutViewModels.SecurityViewModels;

namespace InnovaApp.Portals.MVC4Portal.Controllers.ViewControllers.LayoutViewControllers.SecurityControllers
{
    /// <summary>
    /// Class ResetForgottenPasswordController
    /// </summary>
    public class ResetForgottenPasswordController : _SecurityControllers_Base<ResetForgottenPassword_ViewModel>
    {
        /// <summary>
        /// Gets or sets the forms service.
        /// </summary>
        /// <value>The forms service.</value>
        public IFormsAuthenticationService FormsService { get; set; }
        /// <summary>
        /// Gets or sets the membership service.
        /// </summary>
        /// <value>The membership service.</value>
        public IMembershipService MembershipService { get; set; }

        /// <summary>
        /// Initializes data that might not be available when the constructor is called.
        /// </summary>
        /// <param name="requestContext">The HTTP context and route data.</param>
        protected override void Initialize(RequestContext requestContext)
        {
            base.eMethodBase = "InnovaApp.Layers.SecurityLayer.Controllers.ResetForgottenPasswordController";
            base.specificCssCollection = new string[] { "Views/Account/AccountCommon.css", "ClientComponents/KendoUI/kendo." + PortalEnvironment.KendoUITheme + ".min.css" };
            base.specificJSCollection = null;
            base.dynamicJSCollection = new string[] { "/ScriptsG/ResetForgottenPasswordDynamic.js" };
            base.specificJqueryJSCollection = new string[] { "Views/ResetForgottenPassword.js" };
            base.externalCss = null;
            base.Meta.Add(enMeta.Description, Resources.View_ResetForgottenPassword_Meta_Description);
            base.Meta.Add(enMeta.Keywords, Resources.View_About_Meta_Keywords);
            base.Meta.Add(enMeta.Subject, Resources.View_ResetForgottenPassword_Meta_Subject);

            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        /// <summary>
        /// URL[Get]: /ResetForgottenPassword
        /// </summary>
        /// <returns>ActionResult.</returns>
        [HttpGet]
        public ActionResult Index(string activationcode, string email)
        {
            string language = base.URLReferLanguage;
            base.LocalizeDynamicScript(language, (CurrentContact != null ? CurrentContact.FavoriteLanguage.Language : null));
            using (DataRepositories _dataContext = new DataRepositories())
            {
                base.InitView_Common(
                    pageTitle: Resources.View_ResetForgottenPassword_PageTitle,
                    openingMessage: string.Empty,
                    specificCssCollection: specificCssCollection,
                    specificJSCollection: specificJSCollection,
                    dynamicJSCollection: dynamicJSCollection,
                    specificJqueryJSCollection: specificJqueryJSCollection,
                    jsVariables: jsVariables,
                    externalJs: externalJs,
                    externalCss: externalCss,
                    metaToAdd: Meta,
                    _dataContext: _dataContext,
                    displayMenus: false,
                    language: base.URLReferLanguage,
                    adminMode: false,
                    usesKendoUI: true,
                    jQueryUITheme: null,
                    kendoUITheme: null,
                    currentUser: Membership.GetUser()
                    );
            }
            ContentData.ActivationCode = activationcode;
            ContentData.RegistrationElements.Email = email;
            return View(ContentData);
        }

        /// <summary>
        /// URL[Post]: /ResetForgottenPassword
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        public JsonResult Index(ResetForgottenPassword_ViewModel model)
        {
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };

            //Security code verification
            string username = Membership.GetUserNameByEmail(model.RegistrationElements.Email);
            if (username != null)
            {
                MembershipUser user = Membership.GetUser(username);
                if (user != null)
                {
                    if ((user.Email + user.CreationDate).EncodeSHA1() == model.ActivationCode.Trim())
                    {
                        if (user.ChangePasswordQuestionAndAnswer(model.RegistrationElements.Password, model.RegistrationElements.SecurityQuestion, model.RegistrationElements.SecurityAnswer))
                        {
                            toReturn.Data = new { @success = true, @messageTitle = Resources.View_All_cap_OpeningMessageTitle, @message = Resources.View_ResetForgottenPassword_PasswordChanged, @scriptToExecute = "$.fn.jqDialogAlert(a.messageTitle, a.message, '/', 400,90);" };
                        }
                        else
                        {
                            toReturn.Data = new { @success = false, @messageTitle = Resources.View_All_cap_OpeningMessageTitle, @message = Resources.View_ResetForgottenPassword_ErrorDuringPasswordChange, @scriptToExecute = "$.fn.jqDialogAlert(a.messageTitle, a.message, '/', 400,90);" };
                        }
                    }
                    else
                    {
                        toReturn.Data = new { @success = false, @messageTitle = Resources.View_All_cap_OpeningMessageTitle, @message = Resources.View_ResetForgottenPassword_BadSecurityCode, @scriptToExecute = "$.fn.jqDialogAlert(a.messageTitle, a.message, '/', 400,90);" };
                    }
                }
                else
                {
                    toReturn.Data = new { @success = false, @messageTitle = Resources.View_All_cap_OpeningMessageTitle, @message = Resources.View_ResetForgottenPassword_UnknownAccount, @scriptToExecute = "$.fn.jqDialogAlert(a.messageTitle, a.message, '/', 400,90);" };
                }
            }
            else
            {
                toReturn.Data = new { @success = false, @messageTitle = Resources.View_All_cap_OpeningMessageTitle, @message = Resources.View_ResetForgottenPassword_UnknownEMail, @scriptToExecute = "$.fn.jqDialogAlert(a.messageTitle, a.message, '/', 400,90);" };
            }
            return toReturn;
        }
    }
}
